﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PTBAC2
{
    public partial class MainGui : Form
    {
        //------------------ndluan-GUI---------------
        private Image _img;
        private Image[] _arrImg;
        private string[] _arrFileName = { "shut0.png",
                                            "shut1.png",
                                            "shut2.png" };
        private bool _bFlag;
        private Point _diff;
        //===========================================

        CPTB2 cptb2 = new CPTB2();
        
        public MainGui()
        {
            InitializeComponent();
        }

        private void MainGui_Load(object sender, EventArgs e)
        {
            txta.KeyPress += new KeyPressEventHandler(txtaChiNhapSo_KeyPress);
            txtb.KeyPress += new KeyPressEventHandler(txtChiNhapSo_KeyPress);
            txtc.KeyPress += new KeyPressEventHandler(txtChiNhapSo_KeyPress);

            //--------- ndluan ---------------------------------
            _img = Image.FromFile(@"images\iPad.png");
            _arrImg = new Image[3];
            _bFlag = false;
            _diff = new Point(0, 0);
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
            UpdateStyles();
            ClientSize = new Size(_img.Width, _img.Height);
            for (int i = 0; i < 3; ++i)
            {
                _arrImg[i] = Image.FromFile(@"images\" + _arrFileName[i]);
            }

            picQuit.Image = _arrImg[0];

            BitmapRegion.CreateControlRegion(this, _img as Bitmap);
            //==================================================
        }

        //========================= Trần Ngọc Sang ==========================
        //Xử lý sự kiện chỉ cho phép nhập dữ liệu là chữ số.
        public void txtChiNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '0')
            {
                if (((TextBox)sender).Text == "0" || ((TextBox)sender).Text == "-")
                    e.Handled = true;
            }
            else
            {
                if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
            }
            if (e.KeyChar == '-' && ((TextBox)sender).Text == "")
                e.Handled = false;
            if (e.KeyChar == '.' && !((TextBox)sender).Text.Contains("."))
                e.Handled = false; 
        }

        public void txtaChiNhapSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '0')
            {
                if (((TextBox)sender).Text == "" || ((TextBox)sender).Text == "-")
                    e.Handled = true;
            }
            else
            {
                if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
            }
            if (e.KeyChar == '-' && ((TextBox)sender).Text == "")
                e.Handled = false;
            if (e.KeyChar == '.' && !((TextBox)sender).Text.Contains("."))
                e.Handled = false;
        }

        private void setVisibleN()
        {
            lbx1.Visible = lbx2.Visible = true;
        }

        private void buttinhketqua_Click(object sender, EventArgs e)
        {
            try
            {
                cptb2.HeSoA = float.Parse(txta.Text);
                cptb2.HeSoB = float.Parse(txtb.Text);
                cptb2.HeSoC = float.Parse(txtc.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kiem tra cac he so nhap vao.", "Loi", MessageBoxButtons.OK);
                return;
            }
            cptb2.TinhDelta();
            setVisibleN();
            if (cptb2.Delta > 0)
            {
                cptb2.Tinh2Nghiem();
//                txtkqnghiem1.Text = cptb2.Nghiem1.ToString();
  //              txtkqnghiem2.Text = cptb2.Nghiem2.ToString();
                lbx1.Text = "X1 = " + cptb2.Nghiem1.ToString();
                lbx2.Text = "X2 = " + cptb2.Nghiem2.ToString();
                lbdelta.Text = "Delta = " + cptb2.Delta.ToString() + " > 0";
                lbketluan.Text = "Phuong trinh co 2 nghiem.";
            }
            if (cptb2.Delta == 0)
            {
                cptb2.Tinh1Nghiem();
                //txtkqnghiem1.Text = cptb2.Nghiem1.ToString();
                //txtkqnghiem2.Text = cptb2.Nghiem2.ToString();
                lbx1.Text = "X = " + cptb2.Nghiem1.ToString();
                lbx2.Visible = false;
                lbdelta.Text = "Delta = 0";
                lbketluan.Text = "Phuong trinh co 1 nghiem.";

            }
            if (cptb2.Delta < 0)
            {
                lbx1.Visible = false;
                lbx2.Visible = false;
                lbdelta.Text = "Delta = " + cptb2.Delta.ToString() + " < 0 ";
                lbketluan.Text = "Phuong trinh vo nghiem.";
            }
            
        }
        //=========================================================

        //------------------ ndluan -------------------------------
        private void picQuit_MouseEnter(object sender, EventArgs e)
        {
            picQuit.Image = _arrImg[1];
        }

        private void picQuit_MouseLeave(object sender, EventArgs e)
        {
            picQuit.Image = _arrImg[0];
        }

        private void picQuit_MouseUp(object sender, MouseEventArgs e)
        {
            picQuit.Image = _arrImg[1];
        }

        private void picQuit_MouseDown(object sender, MouseEventArgs e)
        {
            picQuit.Image = _arrImg[2];
        }

        private void MainGui_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _bFlag = true;
                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                _diff.X = p.X - DesktopLocation.X;
                _diff.Y = p.Y - DesktopLocation.Y;
            }
        }

        private void MainGui_MouseMove(object sender, MouseEventArgs e)
        {
            if (_bFlag && (e.Button & MouseButtons.Left) != 0)
            {
                Point p = new Point(e.X, e.Y);
                p = PointToScreen(p);
                p.X -= _diff.X;
                p.Y -= _diff.Y;
                DesktopLocation = p;
            }
        }

        private void MainGui_MouseUp(object sender, MouseEventArgs e)
        {
            _bFlag = false;
        }

        private void picQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //====================================================================
    }
}
